package net.lahuobao.export.service.impl;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import net.lahuobao.export.service.comm.IHelp;
import net.lahuobao.export.service.comm.IStreamGen;
import net.lahuobao.export.service.stream.ITxtGen;

@Service("excelStreamGenService")
@Scope("prototype")
public class ExcelStreamGenImpl  extends CommonServiceImpl implements IStreamGen {

	@Autowired
	private IHelp helpService;
	
	private SXSSFWorkbook workbook ;
	private Sheet sheet;
	
	private int iRowIdx = 0;
	
	@Override
	public <T>boolean createStream(Writer writer, String type,HttpServletResponse response,List<T> dataList,int limit,int index) throws IOException, InstantiationException, IllegalAccessException {
		
		 // 声明一个工作薄
		 response.setContentType("application/text;charset=GBK");
	     int len=dataList.size();
	      
	      if(len>0){
	    	  //表头数据,bug:会多次生成表头
	    	  if(iRowIdx==0){
	    		  String[] headers = helpService.CreateTableHeaderArray(dataList.get(0));
	    		  
	    		  Row row = sheet.createRow(0);
	    	      for (short i = 0; i < headers.length; i++) {
	    	         Cell cell = row.createCell(i);
	    	         //cell.setCellStyle(style);
	    	         XSSFRichTextString text = new XSSFRichTextString(headers[i]);
	    	         cell.setCellValue(text);
	    	      }
	    	      
	    	      iRowIdx++;
	    	  }
	    	  
		      //表内容数据
		      for(int i=0;i<len;i++){
		    	 
		    	 Row row = sheet.createRow(iRowIdx);
		    	 iRowIdx++;
		    	 
		    	 String[] datas = helpService.CreateTableItemArray(dataList.get(i));
		    	 for (short j = 0; j < datas.length; j++) {
	    	         Cell cell = row.createCell(j);
	    	         //cell.setCellStyle(style);
	    	         XSSFRichTextString text = new XSSFRichTextString(datas[j]);
	    	         cell.setCellValue(text);
	    	      }
		      }
	      }
	      //limit : 判断分页上限
	      //判断记录数据是否有limit条
	      if(len<limit){
	    	  //writer.flush();
	    	  return false;
	      }else{
	    	  return true;
	      }
	}
	
	
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		workbook = new SXSSFWorkbook(5000);
		sheet = workbook.createSheet("sheet1");
		// 设置表格默认列宽度为15个字节
	      //sheet.setDefaultColumnWidth((short) 15);
		iRowIdx = 0;
	}
	
	
	
	@Override
	public void finish(HttpServletResponse response) {
		// TODO Auto-generated method stub
		
		try {
			workbook.write(response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
}
